package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: javase
 * @description: 模拟死锁
 * @author: luolidong
 * @create: 2021-06-24 20:55
 * @version: 1.0
 */
public class DeadLock2 {
    private static ReentrantLock lock1 = new ReentrantLock();
    private static ReentrantLock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                boolean tryLock = lock1.tryLock(3, TimeUnit.SECONDS);
                if (tryLock) {
                    lock1.lock();
                    System.out.println("A");

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println("获取不到锁");
            }
        }).start();

        new Thread(() -> {
            lock1.lock();
            //
            try {
                while (true) {

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock1.unlock();
            }
        }).start();
    }
}
